package t01;

import java.util.Random;

public class Dice {
    public static void Dice() {
        Random random = new Random();
        int[] sums = new int[11];// 储存骰子点数之和为2-12的数组
        // 摇36000次骰子
        for (int i = 0; i < 36000; i++) {
            int dice1 = random.nextInt(6) + 1;
            int dice2 = random.nextInt(6) + 1;
            int sum = dice1 + dice2;// 便于给sums数组对应的下标计数
            sums[sum - 2]++;
        }
        System.out.println("value\ttimes\tfrequency");// 开始输出表格
        for (int i = 0; i < sums.length; i++) {
            int sum = i + 2;// 从2开始输出
            double exceptedFrequency = getExceptedFrequency(sums, i);//
            System.out.println(sum + "\t\t" + sums[i] + "\t\t" + exceptedFrequency);
        }
    }

    private static double getExceptedFrequency(int[] sums, int i) {
        return (sums[i] / 36000.0);//当前点数出现次数/摇骰子次数==所占百分比
    }

    public static void main(String[] args) {
        Dice();
    }
}
